﻿using System;
using System.Threading;
using System.Windows.Documents;
using SQLQuery.Common;
using System.Windows.Media;

namespace SQLQuery.UI
{
    /// <summary>
    /// LogControl.xaml 的交互逻辑
    /// </summary>
    [System.ComponentModel.DesignTimeVisible(false)]
    public partial class LogControl
    {
        public LogControl()
        {
            InitializeComponent();
        }

        public void WriteLog(string msg, Color errorColor)
        {
            this.InvokeEx(() =>
                  {
                      if (this.cbIsClearState.IsChecked == true)
                      {
                          ClearTextNextLength(5000);
                      }
                      SetTextColor(string.Format("<--------{0}-------->", DateTime.Now.ToString("HH:mm:ss")), 
                          errorColor);
                      SetTextColor(Environment.NewLine + msg + Environment.NewLine, errorColor);

                      ScrollToEnd();
                  });
        }

        private void BtnClearLogItemClick(object sender, DevExpress.Xpf.Bars.ItemClickEventArgs e)
        {
            this.tblog.Document.Blocks.Clear();
            //this.richEditControl1.Text = string.Empty;
        }

        public void ScrollToEnd()
        {
            this.InvokeEx(() =>
                              {
                                  this.tblog.ScrollToEnd();
                              });
            //try
            //{
            //    if (!this.richEditControl1.IsFocused)
            //    {
            //        this.richEditControl1.Focus();
            //    }
            //    this.richEditControl1.ScrollToCaret();
            //}
            //catch { }
        }

        private void SetTextColor(string msg, Color c,float? size = null)
        {
            SolidColorBrush brush = new SolidColorBrush(c);
            TextRange textrange = new TextRange(tblog.Document.ContentEnd, tblog.Document.ContentEnd);
            textrange.Text = msg;
            if (size != null)
            {
                textrange.ApplyPropertyValue(TextElement.FontSizeProperty, size.ToString());
            }
            textrange.ApplyPropertyValue(TextElement.ForegroundProperty, brush);
          
            //DocumentRange range = this.richEditControl1.Document.AppendText(msg);
            //CharacterProperties paragraph = this.richEditControl1.Document.BeginUpdateCharacters(range);
            //paragraph.ForeColor = c;
            //if (size != null)
            //{
            //    paragraph.FontSize = size;
            //}
            //this.richEditControl1.Document.EndUpdateCharacters(paragraph);
            //this.richEditControl1.Document.CaretPosition = range.End;
        }

        private void ClearTextNextLength(int length)
        {
            TextRange tr = new TextRange(tblog.Document.ContentStart, tblog.Document.ContentEnd);
            if (tr.Text.Length > length)
            {
                this.tblog.Document.Blocks.Clear();
            }

            //if (this.richEditControl1.Text.Length > length)
            //{
            //    this.richEditControl1.Text = string.Empty;
            //}
        }
    }
}
